﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLayer;
using BusinessLayer.DTO;

namespace PayRoll
{
    public partial class Attendance : TransactionMasterForm
    {
        public Attendance()
        {
            InitializeComponent();
            
        }

        private void btnSignIn_Click(object sender, EventArgs e)
        {
            try
            {
                ClsAttendance ClsAttendance = new ClsAttendance();
                ClsAttendance.SaveEmployeeAttendance("IN", Utility.LoginEmployeeID,DateTime.Now);
                SetTodaysData();
                MessageBox.Show("Sign in Successfull", "Signed In", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

       
        private void Attendance_Load(object sender, EventArgs e)
        {

            try
            {
                this.Left = 0;
                this.Top = 0;
                this.WindowState = FormWindowState.Maximized;
                ClsWorkingHours ClClsWorkingHours = new ClsWorkingHours();
                bool found = ClClsWorkingHours.GetCurrentWorkingHour(DateTime.Now, DateTime.Now);
                if (found)
                {
                    string fromDate = Utility.GetDateToDisplay(ClClsWorkingHours.DateFrom);
                    string toDate = Utility.GetDateToDisplay(ClClsWorkingHours.DateTo);
                    string fromTime = Utility.GetTimeToDisplay(ClClsWorkingHours.StartWorkingTime);
                    string toTime = Utility.GetTimeToDisplay(ClClsWorkingHours.OffWorkingTime);
                    string gracePeriod = ClClsWorkingHours.MinimumWorkingHour.ToString();
                    decimal noOfHours = ClClsWorkingHours.NoOfHours;
                    //string info = "From " + fromDate + " to " + toDate + " Working hour is from " + fromTime + " to " + toTime + " and Minimum working hour is " + gracePeriod + " hours";
                    //richTextBox1.Text = info;

                    richTextBoxWorkingHour.Text = "";
                    richTextBoxWorkingHour.SelectionColor = Color.Blue;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(" From ");                    
                
                    richTextBoxWorkingHour.SelectionColor = Color.Brown;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(fromDate);

                    richTextBoxWorkingHour.SelectionColor = Color.Blue ;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(" to ");

                    richTextBoxWorkingHour.SelectionColor = Color.Brown;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(toDate);

                    richTextBoxWorkingHour.SelectionColor = Color.Blue;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(" working hour is from ");

                    richTextBoxWorkingHour.SelectionColor = Color.Brown;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(fromTime);

                    richTextBoxWorkingHour.SelectionColor = Color.Blue;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(" to ");

                    richTextBoxWorkingHour.SelectionColor = Color.Brown;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(toTime); 

                    //richTextBoxWorkingHour.SelectionColor = Color.Blue;
                    //richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    //richTextBoxWorkingHour.AppendText(" and minimum working hour is  ");

                    //richTextBoxWorkingHour.SelectionColor = Color.Brown;
                    //richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    //richTextBoxWorkingHour.AppendText(gracePeriod);

                    //richTextBoxWorkingHour.SelectionColor = Color.Blue;
                    //richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    //richTextBoxWorkingHour.AppendText(" hours."); 
               
                }
                else
                {
                    MessageBox.Show("Working hour is not Configured. Please contact Administrator", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    richTextSignIN.Text = "";
                    richTexSignOut.Text = "";
                    richTextBoxWorkingHour.Text = "";
                    btnSignIn.Enabled = false;
                    btnSignOut.Enabled = false;
                    return;
                }
                SetTodaysData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }
        private void SetTodaysData()
        {
            ClsAttendance objClsAttendance = new ClsAttendance();
            objClsAttendance.GetEmployeeAttendance(Utility.LoginEmployeeID);
            if (objClsAttendance.DateAttended.HasValue == true)
            {
                btnSignIn.Enabled = false;
                btnSignOut.Enabled = true;
            }
            else
            {
                btnSignIn.Enabled = true;
                btnSignOut.Enabled = false;
            }

            richTextSignIN.Text = "";            
            richTextSignIN.SelectionColor = Color.Blue;
            richTextSignIN.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
            richTextSignIN.AppendText(" Your todays IN Time is:-  ");

            richTextSignIN.SelectionColor = Color.Brown;
            richTextSignIN.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
            richTextSignIN.AppendText(Utility.GetTimeToDisplay(objClsAttendance.StartTime));


            richTexSignOut.Text = "";
            richTexSignOut.SelectionColor = Color.Blue;
            richTexSignOut.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
            richTexSignOut.AppendText(" Your todays OUT Time is:-  ");

            richTexSignOut.SelectionColor = Color.Brown;
            richTexSignOut.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
            richTexSignOut.AppendText(Utility.GetTimeToDisplay(objClsAttendance.EndTime));
           
            btnSubmit_Click(null, null);
        }
        private void btnSignOut_Click(object sender, EventArgs e)
        {
            try
            {
                ClsAttendance ClsAttendance = new ClsAttendance();
                ClsAttendance.SaveEmployeeAttendance("OUT", Utility.LoginEmployeeID, DateTime.Now);
                SetTodaysData();
                MessageBox.Show("Sign out Successfull", "Signed Out", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            ClsAttendance objClsAttendance = new ClsAttendance();
            DataTable dt = objClsAttendance.GetEmployeeAttendance(Utility.LoginEmployeeID, Convert.ToDateTime(dtdFromDate.Text), Convert.ToDateTime(dtdToDate.Text));

            List<AttendanceInfo> lstAttendanceInfo = new List<AttendanceInfo>();
            ClsWorkingHours clsWorkingHours = new ClsWorkingHours();
            foreach (DataRow dr in dt.Rows)
            {
                AttendanceInfo objAttendanceInfo = new AttendanceInfo();
                objAttendanceInfo.AttendanceID = Convert.ToInt32(dr["AttendanceID"]);
                objAttendanceInfo.EmployeeID = Convert.ToInt32(dr["EmployeeID"].ToString());
                objAttendanceInfo.DateAttended = Utility.GetValidDateElseNull(dr["DateAttended"].ToString());
                objAttendanceInfo.StartTime = Utility.GetValidDateElseNull(dr["StartTime"]);
                objAttendanceInfo.EndTime = Utility.GetValidDateElseNull(dr["EndTime"]);
                objAttendanceInfo.DayName = objAttendanceInfo.DateAttended.Value.DayOfWeek.ToString();
                objAttendanceInfo.NoOfHours = Utility.GetNoOfHours(objAttendanceInfo.StartTime, objAttendanceInfo.EndTime);
                clsWorkingHours = new ClsWorkingHours();
                bool found = clsWorkingHours.GetCurrentWorkingHour(objAttendanceInfo.DateAttended.Value, objAttendanceInfo.DateAttended.Value);
                if (found)
                {
                    decimal overTime = objAttendanceInfo.NoOfHours - clsWorkingHours.NoOfHours;
                    if (overTime > 0)
                        objAttendanceInfo.OverTime = overTime;
                    else
                        overTime = 0;
                }
                lstAttendanceInfo.Add(objAttendanceInfo);
            }
            grdAttendance.DataSource = lstAttendanceInfo.OrderBy(d=> d.DateAttended).ToList();
        }

        private void Attendance_FormClosing(object sender, FormClosingEventArgs e)
        {
            Control[] Cont = this.ParentForm.Controls.Find("groupBox1", false);
            ((GroupBox)Cont[0]).Visible = true;
            Control[] Cont1 = this.ParentForm.Controls.Find("groupBox2", false);
            ((GroupBox)Cont1[0]).Visible = true;
        }

        
    }
}
